🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / repository / EventFirmwareRepositoryImpl.kt
Displaying Raw • Download
core/data/src/commonMain/kotlin/org/meshtastic/core/data/repository/EventFirmwareRepositoryImpl.kt c2a8e67aee4db8f77b7dc6924dcebfbdfba1bf5e (c2a8e67a) Text, 7.22 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.repository
Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787kotlinx.coroutines.Deferred
Tff7b72import T7ee787kotlinx.coroutines.SupervisorJob
Tff7b72import T7ee787kotlinx.coroutines.async
Tff7b72import T7ee787kotlinx.coroutines.sync.Mutex
Tff7b72import T7ee787kotlinx.coroutines.sync.withLock
Tff7b72import T7ee787kotlinx.coroutines.withContext
Tff7b72import T7ee787kotlinx.coroutines.withTimeoutOrNull
Tff7b72import T7ee787kotlinx.serialization.json.Json
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.common.util.nowMillis
Tff7b72import T7ee787org.meshtastic.core.common.util.safeCatching
Tff7b72import T7ee787org.meshtastic.core.data.datasource.BundledAssetReader
Tff7b72import T7ee787org.meshtastic.core.data.datasource.EventFirmwareEditionLocalDataSource
Tff7b72import T7ee787org.meshtastic.core.data.datasource.decode
Tff7b72import T7ee787org.meshtastic.core.database.entity.asEntity
Tff7b72import T7ee787org.meshtastic.core.database.entity.asExternalModel
Tff7b72import T7ee787org.meshtastic.core.di.CoroutineDispatchers
Tff7b72import T7ee787org.meshtastic.core.model.EventFirmwareEdition
Tff7b72import T7ee787org.meshtastic.core.model.EventFirmwareResponse
Tff7b72import T7ee787org.meshtastic.core.model.util.TimeConstants
Tff7b72import T7ee787org.meshtastic.core.network.EventFirmwareRemoteDataSource
Tff7b72import T7ee787org.meshtastic.core.repository.EventFirmwareRepository
Tff7b72import T7ee787kotlin.concurrent.Volatile
Tff7b72import T7ee787kotlin.time.Duration.Companion.minutes
T8b949e/**
* Caches event-firmware display metadata in the DB, seeded from the bundled `event_firmware.json` snapshot and
* refreshed from `/resource/eventFirmware`. DB-backed (not just in-memory) so the snapshot survives across process
* restarts without waiting on a fresh network round-trip. Mirrors [DeviceHardwareRepositoryImpl]'s seed → single-flight
* refresh pattern.
*/
Tf0883e@Single
Tff7b72class T56d364EventFirmwareRepositoryImplTb4b4b4(
Tff7b72private Tff7b72val Te6edf3remoteDataSourceTb4b4b4: Te6edf3EventFirmwareRemoteDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3assetReaderTb4b4b4: Te6edf3BundledAssetReaderTb4b4b4,
Tff7b72private Tff7b72val Te6edf3jsonTb4b4b4: Te6edf3JsonTb4b4b4,
Tff7b72private Tff7b72val Te6edf3localDataSourceTb4b4b4: Te6edf3EventFirmwareEditionLocalDataSourceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3dispatchersTb4b4b4: Te6edf3CoroutineDispatchersTb4b4b4,
Tb4b4b4) Tb4b4b4: Te6edf3EventFirmwareRepository Tb4b4b4{
T8b949e/** Serializes seeding so concurrent callers don't duplicate writes. */
Tff7b72private Tff7b72val Te6edf3writeMutex Tff7b72= Te6edf3MutexTb4b4b4(Tb4b4b4)
Tf0883e@Volatile Tff7b72private Tff7b72var Te6edf3lastRefreshMillis Tff7b72= T79c0ff0L
T8b949e// Advanced on every refresh *attempt* (success, empty, or failure), unlike lastRefreshMillis which only advances on
T8b949e// a stored non-empty payload. Without this, an offline device (a common, expected state) would re-enter the stale
T8b949e// branch and block up to NETWORK_REFRESH_TIMEOUT_MS on every getEdition() call, since the failed Deferred is no
T8b949e// longer active. The cooldown caps retries while offline without waiting the full CACHE_EXPIRATION_TIME_MS.
Tf0883e@Volatile Tff7b72private Tff7b72var Te6edf3lastAttemptMillis Tff7b72= T79c0ff0L
T8b949e/** Guards [inFlightRefresh] so concurrent callers share one network fetch. */
Tff7b72private Tff7b72val Te6edf3refreshGuard Tff7b72= Te6edf3MutexTb4b4b4(Tb4b4b4)
Tff7b72private Tff7b72var Te6edf3inFlightRefreshTb4b4b4: Te6edf3DeferredTff7b72<Tffa657UnitTff7b72>Tff7b72? Tff7b72= Tff7b72null
T8b949e// This @Single lives for the entire app lifetime, so the SupervisorJob is never cancelled. The refresh runs
T8b949e// here so a caller that stops waiting can't abort the shared fetch — api.meshtastic.org has been measured
T8b949e// taking 20-60s, and the fetch should still land in the DB for the next lookup.
Tff7b72private Tff7b72val Te6edf3refreshScope Tff7b72= Te6edf3CoroutineScopeTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3io Tff7b72+ Te6edf3SupervisorJobTb4b4b4(Tb4b4b4)Tb4b4b4)
Tff7b72override Tff7b72suspend Tff7b72fun Td2a8ffgetEditionTb4b4b4(Te6edf3editionNameTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3EventFirmwareEdition? Tff7b72= Te6edf3withContextTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3ioTb4b4b4) Tb4b4b4{
Te6edf3ensureSeededTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3stale Tff7b72= Te6edf3nowMillis Tff7b72- Te6edf3lastRefreshMillis Tff7b72> Te6edf3CACHE_EXPIRATION_TIME_MS
Tff7b72val Te6edf3retryCooldownElapsed Tff7b72= Te6edf3nowMillis Tff7b72- Te6edf3lastAttemptMillis Tff7b72> Te6edf3REFRESH_RETRY_COOLDOWN_MS
Tff7b72if Tb4b4b4(Te6edf3stale Tff7b72&Tff7b72& Te6edf3retryCooldownElapsedTb4b4b4) Tb4b4b4{
Te6edf3singleFlightRefreshTb4b4b4(Te6edf3maxWaitMs Tff7b72= Te6edf3NETWORK_REFRESH_TIMEOUT_MSTb4b4b4)
Tb4b4b4}
Te6edf3localDataSourceTb4b4b4.Te6edf3getByEditionTb4b4b4(Te6edf3editionNameTb4b4b4)Tff7b72?.Te6edf3asExternalModelTb4b4b4(Tb4b4b4)
Tb4b4b4}
T8b949e/** Seeds the table from the bundled snapshot if empty (fresh install, data clear). */
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffensureSeededTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72> T79c0ff0Tb4b4b4) Tff7b72return
Te6edf3writeMutexTb4b4b4.Te6edf3withLock Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3localDataSourceTb4b4b4.Te6edf3countTb4b4b4(Tb4b4b4) Tff7b72=Tff7b72= T79c0ff0Tb4b4b4) Tb4b4b4{
Te6edf3safeCatching Tb4b4b4{
Tff7b72val Te6edf3editions Tff7b72= Te6edf3assetReaderTb4b4b4.Te6edf3decodeTff7b72<Te6edf3EventFirmwareResponseTff7b72>Tb4b4b4(Te6edf3ASSET_NAMETb4b4b4, Te6edf3jsonTb4b4b4)Tff7b72?.Te6edf3editionsTb4b4b4.Te6edf3orEmptyTb4b4b4(Tb4b4b4)
Te6edf3localDataSourceTb4b4b4.Te6edf3upsertAllTb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3asEntityTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4)
Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffEventFirmwareRepository: failed to seed from bundled JSONTa5d6ff" Tb4b4b4} Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Starts (or joins) a single shared refresh running in [refreshScope]. The caller waits at most [maxWaitMs] before
* falling back to cached data; the refresh itself always runs to completion, bounded only by the HttpClient's own
* timeout/retry policy.
*/
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffsingleFlightRefreshTb4b4b4(Te6edf3maxWaitMsTb4b4b4: Tffa657LongTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3refresh Tff7b72=
Te6edf3refreshGuardTb4b4b4.Te6edf3withLock Tb4b4b4{
Te6edf3inFlightRefreshTff7b72?.Te6edf3takeIf Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3isActive Tb4b4b4}
Tff7b72?: Te6edf3refreshScope
Tb4b4b4.Te6edf3async Tb4b4b4{
Te6edf3lastAttemptMillis Tff7b72= Te6edf3nowMillis
Te6edf3safeCatching Tb4b4b4{
Tff7b72val Te6edf3editions Tff7b72= Te6edf3remoteDataSourceTb4b4b4.Te6edf3getEventFirmwareTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3editions
Tff7b72if Tb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3isNotEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3localDataSourceTb4b4b4.Te6edf3upsertAllTb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3asEntityTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4)
Te6edf3localDataSourceTb4b4b4.Te6edf3deleteNotInTb4b4b4(Te6edf3editionsTb4b4b4.Te6edf3map Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3edition Tb4b4b4}Tb4b4b4)
Te6edf3lastRefreshMillis Tff7b72= Te6edf3nowMillis
Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffEventFirmwareRepository: network refresh failedTa5d6ff" Tb4b4b4} Tb4b4b4}
Tffa657Unit
Tb4b4b4}
Tb4b4b4.Te6edf3also Tb4b4b4{ Te6edf3inFlightRefresh Tff7b72= Tffa657it Tb4b4b4}
Tb4b4b4}
Tff7b72if Tb4b4b4(Te6edf3withTimeoutOrNullTb4b4b4(Te6edf3maxWaitMsTb4b4b4) Tb4b4b4{ Te6edf3refreshTb4b4b4.Te6edf3joinTb4b4b4(Tb4b4b4) Tb4b4b4} Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Ta5d6ffEventFirmwareRepository: refresh still in flight after Tffd700${Te6edf3maxWaitMsTffd700}Ta5d6ffms; using cached dataTa5d6ff" Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tff7b72private Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72private Tff7b72const Tff7b72val Te6edf3ASSET_NAME Tff7b72= Ta5d6ff"Ta5d6ffevent_firmware.jsonTa5d6ff"
Tff7b72private Tff7b72val Te6edf3CACHE_EXPIRATION_TIME_MS Tff7b72= Te6edf3TimeConstantsTb4b4b4.Te6edf3ONE_DAYTb4b4b4.Te6edf3inWholeMilliseconds
T8b949e/** Minimum gap between refresh *attempts*, so a failing/empty fetch (e.g. offline) doesn't retry every call. */
Tff7b72private Tff7b72val Te6edf3REFRESH_RETRY_COOLDOWN_MS Tff7b72= T79c0ff5.Te6edf3minutesTb4b4b4.Te6edf3inWholeMilliseconds
T8b949e/** Maximum time a blocking lookup waits for an in-flight refresh before returning cached/bundled data. */
Tff7b72private Tff7b72const Tff7b72val Te6edf3NETWORK_REFRESH_TIMEOUT_MS Tff7b72= T79c0ff5Te6edf3_000L
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.06s